<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The controller base class is where the code which uses models and views will be.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Core.Application
 */
abstract class Dmf_Application_Controller
{
    /**
     * The request class
     * 
     * @var Dmf_Request
     */
    public $request;
    
    /**
     * The view
     * 
     * @var Dmf_Resource 
     */
    public $view;
    
    /**
     * Even if you've disabled layouts it's still used
     * but then a empty one, this saves a lot of code.
     *  
     * @var Dmf_Layout 
     */
    public $layout;
    
    /**
     * The layout file when empty the default defined
     * in the settings file will be used.
     * 
     * @var string 
     */
    public $layout_file;
    
    /**
     * The configuration object
     * 
     * @var Object
     */
    public $config;
    
    /**
     * Get the loaded controller instance
     * 
     * @var Dmf_Application_Controller
     */
    private static $_instance;
    
    /**
     * Get the controller instance
     * 
     * @return Dmf_Application_Controller
     */
    public static function getInstance()
    {
        return self::$_instance;
    }
    
    /**
     * Constructor
     * 
     * @param Dmf_Request $request 
     * @param Dmf_Template $template
     */
    public function __construct( Dmf_Request $request, Dmf_Template $template )
    {
        $this->request = $request;
        $this->layout = $template->layout;
        $this->view = $template->view;
        $this->config = Dmf_Application::getInstance()->getConfig()->asObject();
        
        self::$_instance = $this;
        
        $this->_createHelpers();
    }
    
    /**
     * These helper functions can be used to return
     * in controllers and execute a command withouth
     * loading the layout or view.
     */
    private function _createHelpers()
    {
        if ( function_exists( 'Plain' ) )
        {
            return;
        }
        
        function Plain( $text )
        {
            return $text;
        }
        
        function Json( array $array )
        {
            return json_encode( $array, true );
        }
        
        function Redirect( $controller, $action )
        {
            header( 'Location: ' . Dmf_Application::getInstance()->getConfig()->asObject()->application->public_path . $controller . '/' . $action );
            return false;
        }
        
        function RedirectUrl( $url )
        {
            header( 'Location: ' . $url );
            return false;
        }
        
        function Image( $file, $mime = 'image/jpeg' )
        {
            header( 'Content-Type: ' . $mime );
            
            if ( is_resource( $file ) )
            {
                return $file;
            }
            
            return file_get_contents( $file );
        }
        
        function Download( $file, $mime = 'text/plain' )
        {
            if ( ! file_exists( $file ) || ! is_readable( $file ) )
            {
                throw new Exception( 'Could not offer file: "' . $file . '" for download.' );
            }
            
            header( 'Content-Type: ' . $mime );
            header( 'Content-Disposition: attachment; filename=' . $file );
            
            $fh = fopen( $file, 'r' );
            
            while( ! feof( $fh ) )
            {
                echo fread( $fh, 2048 );
            }
            
            fclose( $fh );
            return false;
        }
    }
    
    /**
     * Get session instance
     * 
     * @param string $name 
     * @return Dmf_Sessions
     */
    public function session( $name )
    {
        return Dmf_Sessions::init( $name );
    }
    
    /**
     * This method will be called before any action is called.
     */
    public function __init()
    {
        
    }
}